package com.treasure.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.treasure.common.model.PageResult;
import com.treasure.infrastructure.model.po.CorpUserInfo;
import com.treasure.infrastructure.model.po.ModelUsageDetails;
import com.treasure.infrastructure.repository.dao.mapper.ModelUsageDetailsMapper;
import com.treasure.web.command.PageConvertor;
import com.treasure.web.command.request.ModelUsageDetailRequest;
import com.treasure.web.command.response.ModelUsageDetailsResponse;
import com.treasure.web.service.ICorpInfoService;
import com.treasure.web.service.ICorpUserInfoService;
import com.treasure.web.service.IModelUsageDetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sunhongmin
 * @since 2023-11-11
 */
@Service
@RequiredArgsConstructor
public class ModelUsageDetailsServiceImpl extends ServiceImpl<ModelUsageDetailsMapper, ModelUsageDetails> implements IModelUsageDetailsService {

    private final ModelUsageDetailsMapper modelUsageDetailsMapper;
    private final ICorpInfoService corpInfoService;
    private final ICorpUserInfoService corpUserInfoService;

    @Override
    public PageResult<ModelUsageDetailsResponse> modelUsageList(ModelUsageDetailRequest modelUsageDetailRequest) {
        QueryWrapper<ModelUsageDetails> modelUsageQuery = new QueryWrapper<>();
        modelUsageQuery.lambda()
                .orderByDesc(ModelUsageDetails::getCreateTime);
        if (StringUtils.isNotBlank(modelUsageDetailRequest.getCorpId())) {
            modelUsageQuery.lambda().eq(ModelUsageDetails::getCorpId, modelUsageDetailRequest.getCorpId());
        }
        if (Objects.nonNull(modelUsageDetailRequest.getUserName())) {
            List<CorpUserInfo> corpUserInfoList = corpInfoService.queryUserListByNameLike(modelUsageDetailRequest.getUserName(), modelUsageDetailRequest.getCorpId());
            if (CollectionUtils.isEmpty(corpUserInfoList)) {
                return PageConvertor.emptyPage();
            }
            List<String> userIdList = corpUserInfoList.stream().map(CorpUserInfo::getUserId).collect(Collectors.toList());
            modelUsageQuery.lambda().in(ModelUsageDetails::getUserId, userIdList);
        }

        PageHelper.startPage(modelUsageDetailRequest.getPage(), modelUsageDetailRequest.getPageSize());
        List<ModelUsageDetails> modelUsageDetails = modelUsageDetailsMapper.selectList(modelUsageQuery);
        PageInfo<ModelUsageDetails> pageInfo = new PageInfo<>(modelUsageDetails);
        if (CollectionUtils.isEmpty(pageInfo.getList())) {
            return PageConvertor.emptyPage();
        }

        List<String> userIdList = pageInfo.getList().stream().map(ModelUsageDetails::getUserId).collect(Collectors.toList());
        QueryWrapper<CorpUserInfo> userIdInQuery = new QueryWrapper<>();
        userIdInQuery.lambda()
                .in(CorpUserInfo::getUserId, userIdList);
        List<CorpUserInfo> corpUserInfoList = corpUserInfoService.list(userIdInQuery);
        Map<String, String> userIdMap = corpUserInfoList.stream().collect(Collectors.toMap(CorpUserInfo::getUserId, CorpUserInfo::getUsername));

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<ModelUsageDetailsResponse> result = new ArrayList<>(pageInfo.getList().size());
        for (ModelUsageDetails usageDetails : pageInfo.getList()) {
            ModelUsageDetailsResponse modelUsageDetailsResponse = new ModelUsageDetailsResponse();
            modelUsageDetailsResponse.setId(usageDetails.getId());
            modelUsageDetailsResponse.setCorpId(usageDetails.getCorpId());
            modelUsageDetailsResponse.setUserName(userIdMap.get(usageDetails.getUserId()));
            modelUsageDetailsResponse.setModel(usageDetails.getModel());
            modelUsageDetailsResponse.setTokens(usageDetails.getTokens());
            modelUsageDetailsResponse.setCreateTime(formatter.format(usageDetails.getCreateTime()));
            modelUsageDetailsResponse.setRobotName(null);
            modelUsageDetailsResponse.setManufacturer(usageDetails.getManufacturer());
            result.add(modelUsageDetailsResponse);
        }

        return PageConvertor.convertPage(pageInfo, result);
    }
}
